<template>
  <div id="addAddress">
      <van-nav-bar
    title="添加地址"
    left-arrow
    :fixed=true
    :border=true
    @click-left="onClickLeft"
    />
    <van-address-edit
  :area-list="areaList"
  show-postal
  show-set-default
  @save="onSave"
  @delete="onDelete"
  @change-detail="onChangeDetail"
  style="margin-top:3rem"
/>
  </div>
</template>

<script>
import {Toast} from 'vant'
import areaList from './../../../../config/area';
import {addUserAddress} from './../../../../server/api/index'
import {mapState} from 'vuex'
import PubSub from 'pubsub-js'
export default {
  name: 'AddAddress',
  data() { 
    return {
      areaList:areaList,
      searchResult: []
    }
  },
  computed:{
    ...mapState(['userInfo'])
  },
  methods:{
      onClickLeft(){
          this.$router.go(-1);
      },
      async onSave(content) {
         if (this.userInfo.token) {
           let result = await addUserAddress(this.userInfo.token,content.name,content.tel,content.province+content.city+content.county,
           content.addressDetail,content.postalCode,content.isDefault,content.province,content.city,content.county,content.areaCode);
           if (result.success_code === 200) { // 成功
              Toast({
               message:'添加成功',
               duration:400
             })
              this.$router.back();
             // 发起通知
             PubSub.publish('backToMyAddress')
           }else{
             Toast({
               message:'添加失败',
               duration:500
             })
           }
         }
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [{
          name: '黄龙万科中心',
          address: '杭州市西湖区'
        }];
      } else {
        this.searchResult = [];
      }
    }
  }
 }
</script>

<style scoped>
#addAddress{
    position: fixed;
    top: 0;
    left:0;
    right: 0;
    bottom: 0;
    background-color: #f5f5f5;
    z-index: 9999;
}
</style>